// AtomNode.cs created with MonoDevelop
// User: jeremie at 14:50 02/01/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;

using Irony.Compiler;
using Irony.Compiler.AST;

using Microsoft.Linq;
using Microsoft.Linq.Expressions;

namespace Bechamel
{
	public enum AtomNodeType {
		Number,
		String,
		Char,
		Constant,
		Identifier
	}

	// Represent constant
	public class AtomNode : BechamelNode
	{
		AstNode terminal;
		internal AtomNodeType type;
		
		public AtomNode(NodeArgs args, AstNode term, AtomNodeType type)
			: base (args)
		{
			terminal = term;
			this.type = type;

			//Console.WriteLine("Atom debug {0} : {1}", term.GetContent(), type.ToString());
		}

		public override Expression GenerateExpression(ETGenerator generator)
		{
			// Dont take some constant in account yet
			switch (type) {
			case AtomNodeType.Number:
				return Expression.Constant(int.Parse(terminal.GetContent()), typeof(Int32));
			case AtomNodeType.String:
				//Console.WriteLine(terminal.GetContent());
				return Expression.Constant(terminal.GetContent(), typeof(String));
			case AtomNodeType.Char:
				return Expression.Constant(terminal.GetContent()[0], typeof(Char));
			case AtomNodeType.Constant:
				if (terminal.GetContent().ToLower() == "#t")
					return Expression.True();
				else if (terminal.GetContent().ToLower() == "#f")
					return Expression.False();
				return null;
			case AtomNodeType.Identifier:
				return ((IdentifierNode)terminal.ChildNodes[0]).GenerateExpression(generator);
			default:
				return null;
			}
		}
	}
}
